<?php
/**
 * #Celebrate Shieh
 *             ,%%%%%%%%,
 *           ,%%/\%%%%/\%%
 *          ,%%%\c "" J/%%%
 * %.       %%%%/ o  o \%%%
 * `%%.     %%%%    _  |%%%
 *  `%%     `%%%%(__Y__)%%'
 *  //       ;%%%%`\-/%%%'
 * ((       /  `%%%%%%%'
 *  \\    .'          |
 *   \\  /       \  | |
 *    \\/         ) | |
 *     \         /_ | |__
 *     (___________))))))) 攻城湿
 *
 *        _       _
 * __   _(_)_   _(_) __ _ _ __
 * \ \ / / \ \ / / |/ _` |'_ \
 *  \ V /| |\ V /| | (_| | | | |
 *   \_/ |_| \_/ |_|\__,_|_| |_|
 */

namespace App\Services;

use App\Models\System\Role;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoleService
{

    use Service;

    /**
     * 角色列表
     *
     * @param Request $request
     *
     * @return mixed
     */
    public function RoleIndexService( $request )
    {
        $where  = function ( $query ) use ( $request ) {
            if ( $request->filled('name') and $request->input('name') != '' ) $query->where('name', $request->input('name'));
        };
        $select = [ 'id', 'name', 'updated_at', 'created_at' ];
        $role   = Role::PaginateModel($where, $select);
        $role->appends([ 'name' => $request->input('name') ])->render();//赋值$request
        return view('system.role.index', compact('role', 'request'));
    }

    /**
     * 角色新增
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function RoleStoreService( $request )
    {
        $validator = $this->getAdminValidator()->RoleStoreValidator($request);
        if ( $validator->first() ) return back()->with('error', $validator->first());
        DB::transaction(function () use ( $request ) {
            $role = Role::CreateModel($request->all());
            $role->permissions()->sync($request->input('permission_id'));
        });
        return back()->with('success', $this->getErrorCode()::message(209));
    }

    /**
     * 编辑角色
     *
     * @param $id
     *
     * @return mixed
     */
    public function RoleEditService( $id )
    {
        $select           = [ 'id', 'name' ];
        $role             = Role::FindModel($id, $select);
        $role_permissions = $role->permissions->pluck('id');
        return view('system.role.edit', compact('role', 'role_permissions'));
    }

    /**
     * 更新角色
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function RoleUpdateService( $request )
    {
        $result = DB::transaction(function () use ( $request ) {
            $validator = $this->getAdminValidator()->RoleUpdateValidator($request);
            if ( $validator->first() ) return back()->with('error', $validator->first());
            $select     = [ 'id', 'name' ];
            $role       = Role::FindModel($request->input('id'), $select);
            $role->name = $request->input('name');
            $role->save();
            $role->permissions()->sync($request->input('permission_id'));
            return back()->with('success', $this->getErrorCode()::message(203));
        });
        return $result;
    }

    /**
     * 删除角色
     *
     * @param $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function RoleDestroyService( $id )
    {
        $error = $this->getErrorCode();
        $name  = Role::ValueModel($id, 'name');
        if ( $name === '超级管理员' ) return back()->with('error', $error::message(1026));
        DB::transaction(function () use ( $error, $id ) {
            Role::DetachModel($id);
            Role::DestroyModel($id);
        });
        return back()->with('success', $error::message(204));
    }

    /**
     * 批量删除角色
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function RoleDeleteService( $request )
    {
        $ids = $request->input('id');
        foreach ( $ids as $value ) {
            $name = Role::ValueModel($value, 'name');
            if ( $name === '超级管理员' ) return message($this->getErrorCode()::message(1026));
        }
        DB::transaction(function () use ( $ids ) {
            foreach ( $ids as $value ) Role::DetachModel($value);
            Role::DestroyModel($ids);
        });
        return data();
    }

}
